package com.primestone.cim.async;

import java.util.List;
import java.util.Random;
import java.util.logging.Logger;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.primestone.cim.entities.tmp.ResMeterReadings;

/**
 * Session Bean implementation class TmpTableEJBBean
 */
@TransactionManagement( TransactionManagementType.CONTAINER )
@Stateless(mappedName = "TmpTableEJB", name = "TmpTableEJB")
public class TmpTableEJBBean implements TmpTableEJBLocal {
	
	//private final static Logger LOGGER = Logger.getLogger(TmpTableEJBBean.class.getName());
	
	@PersistenceContext( unitName = "TmpTablesGMR" )
	protected EntityManager em;

    /**
     * Default constructor. 
     */
    public TmpTableEJBBean() {
        // Auto-generated constructor stub
    }
    
    @Override
    @TransactionAttribute( value = TransactionAttributeType.REQUIRES_NEW )
    public Long saveResMeterReadings(ResMeterReadings resMeterReading) {
     	this.em.persist(resMeterReading);
		return resMeterReading.getId();
    }
    
    @Override
    @TransactionAttribute( value = TransactionAttributeType.REQUIRES_NEW )
    public void saveResMeterReadings(List<ResMeterReadings> resMeterReadings) {
    	for (ResMeterReadings resMeterReading : resMeterReadings) {
    		this.em.persist(resMeterReading);
		}
    }
    
    @SuppressWarnings("unchecked")
	@Override
    @TransactionAttribute( value = TransactionAttributeType.NOT_SUPPORTED )
    public List<ResMeterReadings> findResMeterReadingsByTicket(String ticket) {
    	Query query = this.em.createNamedQuery( "ResMeterReadings.findByTicket" );
    	query.setParameter( "ResMeterReadings_ticket", ticket );

    	return query.getResultList();
    }
    
    @Override
    @TransactionAttribute( value = TransactionAttributeType.NOT_SUPPORTED )
    public ResMeterReadings findNextResMeterReadingsByTicket(String ticket, Long id) {
    	try {
    		Query query = this.em.createNamedQuery( "ResMeterReadings.findNextByTicket" );
    		query.setParameter( 1, ticket );
    		query.setParameter( 2, id );

    		return ( ResMeterReadings )query.getSingleResult();

    	} catch( NoResultException e ) {
    		return null;
    	}
    }
    
    @Override
    @TransactionAttribute( value = TransactionAttributeType.NOT_SUPPORTED )
    public ResMeterReadings findFirstResMeterReadingsByTicket(String ticket) {
    	try {
            Query query = this.em.createNamedQuery( "ResMeterReadings.findFirstByTicket" );
            query.setParameter( 1, ticket );

            return ( ResMeterReadings )query.getSingleResult();

        } catch( NoResultException e ) {
            return null;
        }
    }
    
    @Override
    @TransactionAttribute( value = TransactionAttributeType.REQUIRES_NEW )
    public void deleteResMeterReadings(ResMeterReadings resMeterReading) {
    	this.em.remove( this.em.merge( resMeterReading ) );
    }
    
    @Override
    @TransactionAttribute( value = TransactionAttributeType.REQUIRES_NEW )
    public void deleteResMeterReadingsById(Long id) {
    	Query query = this.em.createNamedQuery( "ResMeterReadings.deleteById" )
    	.setParameter("ResMeterReadings_id", id);
    	
    	query.executeUpdate();
    }

}
